package com.gjy.pd.observer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2022-12-06 14:45:27
 */
public class LotteryObservable implements Observable {

    private static final Logger logger = LoggerFactory.getLogger(LotteryObservable.class);
    private final String status;
    private final CopyOnWriteArraySet<Observer> set;

    public LotteryObservable() {
//        this.status = UUID.randomUUID().toString();
        // 可以将生成的 status 存进数据库
        this.status = "UUID";
        this.set = new CopyOnWriteArraySet<>();
        logger.info("\n\nstatus: {}\n\n", status);
    }

    @Override
    public void process(Map<String, Object> map) {
        set.forEach(observer -> observer.process(map));
    }

    @Override
    public boolean attach(Observer observer) {
        return set.add(observer);
    }

    @Override
    public boolean attaches(List<Observer> observers) {
        return set.addAll(observers);
    }

    @Override
    public boolean detach(Observer observer) {
        return set.remove(observer);
    }

    @Override
    public boolean detaches(List<Observer> observers) {
        return set.removeAll(observers);
    }

    public String getStatus() {
        return status;
    }
}
